package com.xhtt.modules.cfg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.modules.cfg.entity.CfgHoseInfo;
import com.xhtt.modules.cfg.entity.CfgPumpInfo;
import com.xhtt.modules.cfg.entity.CfgTubeInfo;
import com.xhtt.modules.cfg.service.CfgConPointService;
import com.xhtt.modules.cfg.service.CfgHoseInfoService;
import com.xhtt.modules.cfg.service.CfgPumpInfoService;
import com.xhtt.modules.cfg.service.CfgTubeInfoService;
import com.xhtt.modules.task.entity.TaskPotTaskStep;
import com.xhtt.modules.task.entity.TaskShipTaskProdStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CfgConPointServiceImpl implements CfgConPointService {
    @Autowired
    private CfgTubeInfoService cfgTubeInfoService;
    @Autowired
    private CfgPumpInfoService cfgPumpInfoService;
    @Autowired
    private CfgHoseInfoService cfgHoseInfoService;

    @Override
    public void releaseShipPoint(List<TaskShipTaskProdStep> steps) {

        List<Integer> tubeIds = new ArrayList<>();
        List<Integer> pumpIds = new ArrayList<>();
        List<Integer> hoseIds = new ArrayList<>();
        for (TaskShipTaskProdStep step : steps) {
            Integer type = step.getType();//1罐2硬3泵4码5软
            if (2 == type.intValue()) {
                tubeIds.add(step.getTypeId());
            } else if (5 == type.intValue()) {
                hoseIds.add(step.getTypeId());
            } else if (3 == type.intValue()) {
                pumpIds.add(step.getTypeId());
            }
        }
        release(tubeIds, pumpIds, hoseIds);
    }

    @Override
    public void releasePotPoint(List<TaskPotTaskStep> steps) {
        List<Integer> tubeIds = new ArrayList<>();
        List<Integer> pumpIds = new ArrayList<>();
        List<Integer> hoseIds = new ArrayList<>();
        for (TaskPotTaskStep step : steps) {
            Integer type = step.getType();//1罐2硬3泵4码5软
            if (2 == type.intValue()) {
                tubeIds.add(step.getTypeId());
            } else if (3 == type.intValue()) {
                pumpIds.add(step.getTypeId());
            } else if (5 == type.intValue()) {
                hoseIds.add(step.getTypeId());
            }
        }
        release(tubeIds, pumpIds, hoseIds);
    }

    private void release(List<Integer> tubeIds, List<Integer> pumpIds, List<Integer> hoseIds) {
        if (tubeIds.size() > 0) {
            CfgTubeInfo tube = new CfgTubeInfo();
            tube.setUsed(0);
            cfgTubeInfoService.update(tube, new LambdaQueryWrapper<CfgTubeInfo>().in(CfgTubeInfo::getId, tubeIds));
        }
        if (pumpIds.size() > 0) {
            CfgPumpInfo pump = new CfgPumpInfo();
            pump.setUsed(0);
            cfgPumpInfoService.update(pump, new LambdaQueryWrapper<CfgPumpInfo>().in(CfgPumpInfo::getId, pumpIds));
        }
        if (hoseIds.size() > 0) {
            CfgHoseInfo hose = new CfgHoseInfo();
            hose.setUsed(0);
            cfgHoseInfoService.update(hose, new LambdaQueryWrapper<CfgHoseInfo>().in(CfgHoseInfo::getId, hoseIds));
        }
    }
}
